package com.practicesuite.storage;

import com.practicesuite.storage.MyQuery;
import java.sql.ResultSet;

/**
 *
 * @author Dan
 */
public class MySignup {

    public int UserLogin(String username, String password) {
        MyInsert ib = new MyInsert();
        MyQuery qb = new MyQuery();
        int loginid = 0;
        String[] userLogin = {username, password};
        ib.executeInsertStatement("userlogin", userLogin);
        String[] check = {username, password};
        String sqlQuery = "select * from mydb.userlogin "
                + "where username = ? AND password = ?";
        ResultSet rs = qb.executeQueryWhere(sqlQuery, check);
        try {
            if (rs.next()) {
                loginid = rs.getInt("loginid");
            }
        } catch (Exception ex) {
            ex.fillInStackTrace();
        } finally {
            qb.closeConnection();
        }
        return loginid;
    }

    public int insertUserInfo(String firstname, String lastname, String emailaddr, String country, int loginid) {
        MyInsert ib = new MyInsert();
        MyQuery qb = new MyQuery();
        int userid = 0;
        String loginidS = Integer.toString(loginid);
        String[] userInfo = {firstname, lastname, emailaddr, country, loginidS};
        String[] where = {loginidS};
        ib.executeInsertStatement("userinfo", userInfo);
        String sqlQuery = "select * from mydb.userinfo "
                + "where loginid = ?";

        ResultSet rs = qb.executeQueryWhere(sqlQuery, where);
        try {
            if (rs.next()) {
                userid = rs.getInt("userid");
            }
        } catch (Exception ex) {
            ex.fillInStackTrace();
        } finally {
            qb.closeConnection();
        }
        return userid;
    }

    public Boolean usernameValidator(String obj) {
        MyQuery qb = new MyQuery();
        boolean uNameUnique = false;
        String uName = obj;
        uName = uName.trim();
        String sqlUname =
                "select username from mydb.UserLogin where username = ?";
        String[] whereU = {uName};
        ResultSet uNameRs = qb.executeQueryWhere(sqlUname, whereU);
        try {
            if (!uNameRs.next()) {
                uNameUnique = true;
            }
        } catch (Exception ex) {
            ex.fillInStackTrace();
        }

        if (!uNameUnique) {
            return false;
        } else {
            return true;
        }
    }
}
